// pages/new-data/index.js
import {
  chemicalAlias
} from '../../dataSource'
import {
  request
} from '../../api/index'

const form = {
  chemicalComposition: {
    name: 'RM1',
    type: '化学成分',
    value: ''
  },
  shrinkage: {
    name: 'RM2',
    type: '收缩率',
    value: ''
  },
  lossOnIgnition: {
    name: 'RM3',
    type: '烧失量',
    value: ''
  },
  waterAbsorption: {
    name: 'RM4',
    type: '吸水率',
    value: ''
  },
  whiteness: {
    name: 'RM5',
    type: '白度',
    value: ''
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    safeArea: 0,
    form,
    fields: Object.keys(form),
    chemicalShow: false,
    canNext: false,
    formResult: {},
    randomValues: [],
    chemicalAlias: [],
    id: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
 
    this.setPageProperty();
    this.setDataSource();
    this.setPageData(options);
    this.fetchData();
  },

  /**
   * 设置页面属性
   */
  setPageProperty() {
    wx.setNavigationBarTitle({
      title: '坯体原料'
    })
  },
  setPageData({
    id
  }) {
    this.setData({
      id: Number(id)
    })
  },
  fetchData() {
    request.rawMaterialGet({
      collectionRecordIds: this.data.id,
    }, (e) => {
      let k = Object.assign({}, this.data.form);
      e[0] && this.data.fields.forEach(item => {
        k[item].value = e[0][item],
        k.id = e[0].id
      })
      this.setData({
        form: k
      })
      this.setDataSource();
    })
  },
  onChange(event) {
    request.rawMaterialAdd(Object.assign({
      collectionRecordId: this.data.id
    }, this.data.formResult, event.detail.value, {
      id: this.data.form.id
    }), this.next)
  },
  next() {
    wx.redirectTo({
      url: '../embryo-powder-spraying/index?id=' + this.data.id,
    })
  },
  setDataSource() {
    const data = Object.keys(chemicalAlias);
    const result = [];
    for(let it of data) {
      if(it !== 'name') {
        result.push(
        {
          text: chemicalAlias[it],
          value: it,
          defaultValue: this.data.form.chemicalComposition?.value ? this.data.form.chemicalComposition.value[it] : ''
        }
        )
      }
    }
    this.setData({
      chemicalAlias: result
    })
  },
  toHome() {
    wx.navigateBack({
      delta: 1,
    })
  },
  chemicalOpenHandle() {
    this.setData({
      chemicalShow: true
    })
  },
  chemicalCloseHandle() {
    this.setData({
      chemicalShow: false
    })
  },
  chemicalSubmitHandle({
    detail
  }) {
    this.setData({
      formResult: {
        chemicalComposition: detail.value,
        
      }
    })
    this.chemicalCloseHandle();
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.onAppRoute(( res ) => {
      if (res.path === 'pages/new-data/index' && res.openType === 'navigateBack') {
        this.fetchData();
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})